import { Component, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ProfitWithdrawService } from '@core/routes-services/profit-withdraw.service';
import { WITHDRAW_STATUS_OPTION } from '../../../constant/dictionary';
import { myValidNull } from '@core/util/string-utils';

@Component({
  selector: 'app-profit-withdraw-edit',
  templateUrl: './profit-withdraw-edit.component.html',
})
export class ProfitWithdrawEditComponent implements OnInit {
  entity: any = {};
  validateForm: FormGroup;
  isLoading: boolean = false;
  cusName: any;

  statusOption: any [] = WITHDRAW_STATUS_OPTION;

  constructor(private fb: FormBuilder,
              private modal: NzModalRef,
              private profitWithdrawService: ProfitWithdrawService,
              public message: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.cusName = this.entity.cusName;
    this.initValidateForm();
    this.initEntity();
  }

  initEntity() {
    if (this.entity.status == 1) {
      this.isLoading = true;
      this.profitWithdrawService.detail(this.entity.id).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.entity = res.data;
          console.log(this.entity);
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.isLoading = false;
      });
    }
  }

  initValidateForm() {
    this.validateForm = this.fb.group({
      cusName: [this.entity.cusName, [Validators.required]],
      status: [this.entity.status, [Validators.required]],
      withdrawPrice: [this.entity.withdrawPrice, [Validators.required]],
      auditingReason: [this.entity.auditingReason, [Validators.maxLength(200)]],
    });
  }

  /**
   * 审核
   * @param id         待更新id
   * @param status      状态status 提现状态 1-待审核 2-通过 3-不通过
   * @param type       通过时付款方式 1 线下付  2 线上付
   */
  submitForm(status, type): void {
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      if (status == 3 && myValidNull(this.validateForm.value.auditingReason)) {
        this.message.warning('请填写审核备注');
        return;
      }
      this.isLoading = true;
      this.profitWithdrawService.auditing(this.entity.id, status, type, this.validateForm.value).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.modal.close(true);
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.isLoading = false;
      });
    }
  }

  reSend(): void {
    this.isLoading = true;
    this.profitWithdrawService.reSend(this.entity.id).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.modal.close(true);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });

  }

  close() {
    this.modal.destroy();
  }
}
